package com.assess.web.controller;


import com.assess.web.model.Assessment;
import com.assess.web.model.Phases;
import com.assess.web.model.Scheme;
import com.assess.web.service.*;
import com.assess.web.util.DateUtil;
import com.assess.web.util.RespParam;
import com.assess.web.vo.SchemeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 抽选设置
 */
@Controller
@RequestMapping("/random")
public class RandomController extends BaseController {
    @Autowired
    private SchemeService schemeService;
    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private PhasesService phasesService;
    @Autowired
    private RandomDataService randomDataService;

    /**
     * 跳转到抽选设置
     *
     * @return
     */
    @RequestMapping("")
    public ModelAndView index(@RequestParam("assessmentId") Integer assessmentId) {
        Assessment assessment = assessmentService.getAssessment(assessmentId);

        ModelAndView view = new ModelAndView("/random/set");
        view.addObject("vo", assessment);
        view.addObject("schemeList", schemeService.listScheme(assessment.getSchemeId()));
        view.addObject("phasesList", phasesService.list(0));
        view.addObject("companyName", companyService.getCompany(assessment.getCompanyId()).getName());
        view.addObject("addressName", addressService.getAddress(assessment.getAddressId()).getName());
        view.addObject("assessmentTime", DateUtil.format(assessment.getAssessmentTime()));
        return view;
    }

    @ResponseBody
    @RequestMapping("save")
    public RespParam<String> save(@RequestParam("assessmentId") Integer assessmentId,
                                  @RequestParam(value = "phasesId", required = false) Integer phasesId) {
        return success(randomDataService.save(assessmentId, phasesId));
    }

    //      for (Phases phases : phasesList) {
//        if (phases.getParentId() == 0) { // 父分类id==0
//            SchemeVo schemeVo = new SchemeVo();
//            schemeVo.setId(phases.getId());
//            schemeVo.setName(phases.getName());
//            schemeVo.setPanentId(phases.getParentId());
//            buildCateTree(schemeVo, phasesList);
//            phanses.add(schemeVo);
//        }
//    }
//
//        for (Scheme scheme : schemeList) {
//        if (scheme.getParentId() == 0) { // 父分类id==0
//            SchemeVo schemeVo = new SchemeVo();
//            schemeVo.setId(scheme.getId());
//            schemeVo.setName(scheme.getName());
//            schemeVo.setPanentId(scheme.getParentId());
//            buildSchemeVo(schemeVo, schemeList);
//            schemes.add(schemeVo);
//        }
//    }
    // 方案
    private void buildSchemeVo(SchemeVo schemeVo, List<Scheme> all) {
        List<SchemeVo> childs = new ArrayList<>();
        for (Scheme cate : all) {
            if (cate.getParentId().equals(schemeVo.getId())) {
                SchemeVo child = new SchemeVo();
                child.setPanentId(cate.getParentId());
                child.setName(cate.getName());
                child.setId(cate.getId());
                buildSchemeVo(child, all);
                childs.add(child);
            }
        }
        if (!childs.isEmpty()) {
            schemeVo.setChilds(childs);
        }
    }

    // 阶段
    private void buildCateTree(SchemeVo schemeVo, List<Phases> all) {
        List<SchemeVo> childs = new ArrayList<>();
        for (Phases cate : all) {
            if (cate.getParentId().equals(schemeVo.getId())) {
                SchemeVo child = new SchemeVo();
                child.setPanentId(cate.getParentId());
                child.setName(cate.getName());
                child.setId(cate.getId());
                buildCateTree(child, all);
                childs.add(child);
            }
        }
        if (!childs.isEmpty()) {
            schemeVo.setChilds(childs);
        }
    }
}
